package com.spider.UI;

import com.spider.bean.Employee;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class EmployeeManagerUI extends JFrame {
    private JFrame frame;
    private JTable table;
    private DefaultTableModel model;
    private JTextField nametextFieldSearch;
    //准备一个静态集合对象，用于存出系统中的全部员工对象信息
    public static ArrayList<Employee> allEmployees = new ArrayList<>();
    //为这个集合对象添加4个员工对象
    static {
        allEmployees.add(new Employee(1, "张三", "男", 18, "12345678901", "Java开发", "2023-01-01", 5000, "IT"));
        allEmployees.add(new Employee(2, "李四", "女", 19, "12345678902", "Java开发", "2023-01-02", 5000, "IT"));
        allEmployees.add(new Employee(3, "王五", "男", 20, "12345678903", "Java开发", "2023-01-03", 5000, "IT"));
        allEmployees.add(new Employee(4, "赵六", "男", 21, "12345678904", "Java开发", "2023-01-04", 5000, "IT"));
    }

    public EmployeeManagerUI() {

    }
    public EmployeeManagerUI(String username) {
        super("欢迎"+username+"!");
        frame = this;
        initialize();
        this.setVisible(true);
    }

    private void initialize() {
        this.setBounds(100, 100, 800, 600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().setLayout(new BorderLayout());

        // 输入框和搜索按钮
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        nametextFieldSearch = new JTextField(20);
        JButton btnSearch = new JButton("搜索");
        JButton btnAdd = new JButton("添加");
        topPanel.add(nametextFieldSearch);
        topPanel.add(btnSearch);
        topPanel.add(btnAdd);

        // 创建表格模型
        model = new DefaultTableModel(
                new Object[][]{},
                new String[]{"ID", "姓名", "性别", "年龄", "电话" ,"职位", "入职日期", "薪水", "部门"}
        ) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false; // 设置所有单元格为不可编辑
            }
        };

        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setRowHeight(30);

        // 右键菜单
        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem editItem = new JMenuItem("编辑");
        JMenuItem deleteItem = new JMenuItem("删除");
        popupMenu.add(editItem);
        popupMenu.add(deleteItem);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) { // 明确检查是否为鼠标右键
                    int row = table.rowAtPoint(e.getPoint());
                    if (row >= 0) {
                        table.setRowSelectionInterval(row, row);
                        popupMenu.show(table, e.getX(), e.getY());
                    }
                }
            }
        });

        // 绑定事件到菜单项
        // 编辑按钮监听器
        editItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = table.getSelectedRow();
                if (selectedRow >= 0) {
                    int id = (Integer) model.getValueAt(selectedRow, 0);
                    // 打开编辑对话框，传递父窗口和选中的行索引
                    new EditEmployeeUI(EmployeeManagerUI.this, selectedRow);
                }
            }
        });
        // 删除按钮监听器
        deleteItem.addActionListener(e -> {
                int selectedRow = table.getSelectedRow();
                if (selectedRow >= 0) {
                    int id = (Integer) model.getValueAt(selectedRow, 0); // 假设ID在第一列
                    //JOptionPane.showMessageDialog(frame, "删除 ID: " + id);
                    // 这里可以添加删除逻辑，比如从模型中移除该行
                    deleteEmployee(id);
                    model.removeRow(selectedRow);
                }
        });

        // 搜索按钮监听器
        btnSearch.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String searchValue = nametextFieldSearch.getText();
                // 这里可以添加搜索逻辑
            }
        });

        // 添加按钮监听器
        btnAdd.addActionListener(e ->  {
            // 弹出一个员工信息的界面出来。
            new AddEmployeeUI(this);
        });

        frame.getContentPane().add(topPanel, BorderLayout.NORTH);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        // 加载初始数据
        for (Employee employee : allEmployees) {
            model.addRow(new Object[]{
                employee.getId(),
                employee.getName(),
                employee.getSex(),
                employee.getAge(),
                employee.getPhone(),
                employee.getPosition(),
                employee.getEntryDate(),
                employee.getSalary(),
                employee.getDepartment()
            });
        }

    }

    private void deleteEmployee(int id) {
        //从集合中删除该员工对象。
        for (int i = 0; i < allEmployees.size(); i++) {
            Employee e = allEmployees.get(i);
            if (e.getId() == id) {
                allEmployees.remove(i);
                break;
            }
        }
    }

    public void addEmloyee(Employee employee) {
        allEmployees.add(employee);
        model.addRow(new Object[]{
                employee.getId(),
                employee.getName(),
                employee.getSex(),
                employee.getAge(),
                employee.getPhone(),
                employee.getPosition(),
                employee.getEntryDate(),
                employee.getSalary(),
                employee.getDepartment()
        });
    }
    protected JTable getTable() {
        return table;
    }
    public static void main(String[] args) {
        new EmployeeManagerUI("admin");
    }


}